<?php

namespace app\index\model;

use think\Db;
use think\Model;

/*
 * 订单
 */

class Order extends Model
{
    //订单类型
    const TYPE_1 = 1; //商品
    const TYPE_2 = 2; //项目
    const TYPE_3 = 3; //VIP卡
    const TYPE_4 = 4; //体验卡
    const TYPE_5 = 5; //限时抢购
    const TYPE_6 = 6; //新限时抢购
    const TYPE_7 = 7; //优享

    //订单状态
    const STATUS_0 = 0; //已取消
    const STATUS_1 = 1; //待支付=未支付，默认值
    const STATUS_2 = 2; //待预约=已支付

    //待服务开始
    const STATUS_3 = 3; //已预约
    const STATUS_4 = 4; //待服务=商家确认预约
    const STATUS_5 = 5; //已拒绝=商家拒绝预约
    const STATUS_6 = 6; //员工开始服务
    //待服务结束

    //服务中开始
    const STATUS_7 = 7; //服务中=客户确认开始服务
    const STATUS_8 = 8; //美容师服务完成
    //服务中结束

    const STATUS_9 = 9; //待评论=已完成=客户确认服务完成
    const STATUS_10 = 10; //待回复=已评论
    const STATUS_11 = 11; //已结束=已回复

    //支付方式
    const ORDTYPE_1 = 1; //现金
    const ORDTYPE_2 = 2; //VIP卡支付
    const ORDTYPE_3 = 3; //支付宝
    const ORDTYPE_4 = 4; //微信
    const ORDTYPE_5 = 5; //网页微信
    const ORDTYPE_6 = 6; //网页支付宝
    const ORDTYPE_7 = 7; //小程序

    //老板查看状态
    const CREATOR_READ_0 = 0; //未查看
    const CREATOR_READ_1 = 1; //已查看

    //员工查看状态
    const STAFF_READ_0 = 0; //未查看
    const STAFF_READ_1 = 1; //已查看


    /*
     * 订单产品信息
     */
    public function ordPro()
    {
        return $this->hasOne('OrdPro', 'ord_id', 'id')->field('ord_id, pro_id, number, price, type, item_type');
    }

    /*
     * 订单对应的客户信息
     */
    public function custom()
    {
        return $this->belongsTo('Custom', 'ctm_id', 'id')->field('id, custom_phone, custom_name, charge_money, real_money, consume_money');
    }

    /*
     * 订单预约的美容师
     */
    public function staff()
    {
        return $this->belongsTo('Staff', 'staff_id', 'id')->field('id, staff_name, telephone, staff_rank');
    }

    /*
     * 订单对应的店铺
     */
    public function store()
    {
        return $this->belongsTo('Store', 'store_id', 'id')->field('id, money');
    }

    /*
     * 根据预约状态返回订单应该的状态
     * 返回预约里面值最小的
     */
    public static function getOrderStatus($order_id, $yuyue_id, $orderStatus, $yuyueStatus)
    {
        $orderRow = Order::with('ordPro')->where('id', '=', $order_id)
            ->field('id')
            ->find();

        $pro_id = $orderRow['ord_pro']['pro_id'];
        $number = $orderRow['ord_pro']['number'];
        $type = $orderRow['ord_pro']['type'];

        $list = Db::name('ctm_staff')
            ->where('order_id', '=', $order_id)
            ->field('id, status')
            ->select();

        if ($type == self::TYPE_2) {
            //项目

            if (count($list) < $number) {
                return $orderStatus;
            } else {
                $array = [];
                foreach ($list as $value) {
                    if ($value['id'] != $yuyue_id) {
                        array_push($array, $value['status']);
                    }
                }

                if (count($array) > 0) {
                    $min = min($array);
                    return min($yuyueStatus, $min);
                } else {
                    return $yuyueStatus;
                }
            }
        } else if ($type == self::TYPE_4) {
            //体验卡

            $expercardItemCount = Db::name('expercard_item')->where('expercard_id', '=', $pro_id)->count();

            if (count($list) < $expercardItemCount) {
                return $orderStatus;
            } else {
                $array = [];
                foreach ($list as $value) {
                    if ($value['id'] != $yuyue_id) {
                        array_push($array, $value['status']);
                    }
                }

                if (count($array) > 0) {
                    $min = min($array);
                    return min($yuyueStatus, $min);
                } else {
                    return $yuyueStatus;
                }
            }
        } else if ($type == self::TYPE_5) {
            //限时抢购

            if (count($list) < $number) {
                return $orderStatus;
            } else {
                $array = [];
                foreach ($list as $value) {
                    if ($value['id'] != $yuyue_id) {
                        array_push($array, $value['status']);
                    }
                }

                if (count($array) > 0) {
                    $min = min($array);
                    return min($yuyueStatus, $min);
                } else {
                    return $yuyueStatus;
                }
            }
        } else if ($type == self::TYPE_6) {
            //限时抢购

            if (count($list) < $number) {
                return $orderStatus;
            } else {
                $array = [];
                foreach ($list as $value) {
                    if ($value['id'] != $yuyue_id) {
                        array_push($array, $value['status']);
                    }
                }

                if (count($array) > 0) {
                    $min = min($array);
                    return min($yuyueStatus, $min);
                } else {
                    return $yuyueStatus;
                }
            }
        }
    }


}